package npf.cn.com.happyshopping.ui;

import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.ImageView;

import npf.cn.com.happyshopping.R;
import npf.cn.com.happyshopping.base.BaseActivity;

public class MainActivity extends BaseActivity implements View.OnClickListener{

    private ImageView botton_icon_home;
    private ImageView botton_icon_category;
    private ImageView botton_icon_cart;
    private ImageView botton_icon_outlets;
    private ImageView botton_icon_mine;
    private HomeFragment homeFragment;
    private CategoryFragment categoryFragment;
    private OutletsFragment outletsFragment;
    private CartFragment cartFragment;
    private MineFragment mineFragment;
    private FragmentManager manager;
    @Override
    protected void initData() {
        manager = getSupportFragmentManager();
        FragmentTransaction transaction = manager.beginTransaction();
        homeFragment = new HomeFragment();
        categoryFragment = new CategoryFragment();
        outletsFragment = new OutletsFragment();
        cartFragment = new CartFragment();
        mineFragment = new MineFragment();
        transaction.add(R.id.home_fragment_container, homeFragment);
        transaction.add(R.id.home_fragment_container, categoryFragment);
        transaction.add(R.id.home_fragment_container, outletsFragment);
        transaction.add(R.id.home_fragment_container, cartFragment);
        transaction.add(R.id.home_fragment_container, mineFragment);
        transaction.commit();
        selectFragment(1);
    }

    @Override
    protected void initView() {
        botton_icon_home = (ImageView) findViewById(R.id.botton_icon_home);
        botton_icon_category = (ImageView) findViewById(R.id.botton_icon_category);
        botton_icon_cart = (ImageView) findViewById(R.id.botton_icon_cart);
        botton_icon_outlets = (ImageView) findViewById(R.id.botton_icon_outlets);
        botton_icon_mine = (ImageView) findViewById(R.id.botton_icon_mine);
        botton_icon_home.setOnClickListener(this);
        botton_icon_category.setOnClickListener(this);
        botton_icon_cart.setOnClickListener(this);
        botton_icon_outlets.setOnClickListener(this);
        botton_icon_mine.setOnClickListener(this);
        botton_icon_home.setImageResource(R.mipmap.bottom_icon_home_selected);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_main;
    }

    @Override
    public void onClick(View view) {
        initDrawable();
        ImageView iv = (ImageView) view;
        switch (view.getId()) {
            case R.id.botton_icon_home:
                selectFragment(1);
                iv.setImageResource(R.mipmap.bottom_icon_home_selected);
                break;
            case R.id.botton_icon_category:
                selectFragment(2);
                iv.setImageResource(R.mipmap.bottom_icon_category_selected);
                break;
            case R.id.botton_icon_outlets:
                selectFragment(3);
                iv.setImageResource(R.mipmap.bottom_icon_outlets_selected);
                break;
            case R.id.botton_icon_cart:
                selectFragment(4);
                iv.setImageResource(R.mipmap.bottom_icon_cart_selected);
                break;
            case R.id.botton_icon_mine:
                selectFragment(5);
                iv.setImageResource(R.mipmap.bottom_icon_mine_selected);
                break;
        }
    }

    private void selectFragment(int i) {
        FragmentTransaction transaction = manager.beginTransaction();
        transaction.hide(homeFragment)
                .hide(categoryFragment)
                .hide(outletsFragment)
                .hide(cartFragment)
                .hide(mineFragment);
        switch (i) {
            case 1:
                transaction.show(homeFragment);
                break;
            case 2:
                transaction.show(categoryFragment);
                break;
            case 3:
                transaction.show(outletsFragment);
                break;
            case 4:
                transaction.show(cartFragment);
                break;
            case 5:
                transaction.show(mineFragment);
                break;
        }
        transaction.commit();
    }

    private void initDrawable() {
        botton_icon_home.setImageResource(R.mipmap.bottom_icon_home_normal);
        botton_icon_category.setImageResource(R.mipmap.bottom_icon_category_normal);
        botton_icon_outlets.setImageResource(R.mipmap.bottom_icon_outlets_normal);
        botton_icon_cart.setImageResource(R.mipmap.bottom_icon_cart_normal);
        botton_icon_mine.setImageResource(R.mipmap.bottom_icon_mine_normal);
    }
}
